<?php //pack11class

class SearchManager {
    
    public static $extraWhere=false;

    public static function setProperties() {
        array_push(Tracer::$traceFrom,__CLASS__);
        $values=array();
        foreach($_POST as $key=>$value) {
            $keyArray=explode("_",$key);
            if(count($keyArray)==2) {
                $property=PropertiesManager::getProperty($keyArray[0]);
                if($property && $value) {
                    $moreless=($keyArray[1]=="from")? ">=":"<=";
                    if($property->group=="price") {
                        $currentValue="";
                        $square=PropertiesManager::getProperty("square");
                        $currency=PropertiesManager::getProperty("object_currency");
                        $showPrice=ObjectsManager::getShowPrice();
                            if($showPrice) {
                                $direct=PropertiesManager::getProperty("price");
                                $fromsum=PropertiesManager::getProperty("cost");
                                $currentValue.="(((`{$fromsum->field}`/`{$square->field}`) is not null and (`{$fromsum->field}`/`{$square->field}`)".$moreless."case `".$currency->field."`";
                                //trace("showPrice");
                            } else {
                                $direct=PropertiesManager::getProperty("cost");
                                $fromsum=PropertiesManager::getProperty("price");
                                //trace("showCost");
                                $currentValue.="(((`{$fromsum->field}`*`{$square->field}`) is not null and (`{$fromsum->field}`*`{$square->field}`)".$moreless."case `".$currency->field."`";
                            }
                        foreach(CurrencyManager::getAllVariants($value) as $variant) {
                            $currentValue.=" when ".$variant->id." then ".$variant->value;
                        }
                        $currentValue.=" end) or `".$direct->field."`".$moreless." case `".$currency->field."`";
                        foreach(CurrencyManager::getAllVariants($value) as $variant) {
                            $currentValue.=" when ".$variant->id." then ".$variant->value;
                        }
                        $currentValue.=" end)";
                        array_push($values,$currentValue);
                    } else {
                        array_push($values,$property->field.$moreless.$value);
                    }
                }
            }
            if($key=="word_search") {
                $ids=self::searchWord($value);
                //if(is_array($ids)) {
                    $table=MySQLGet::process("Show columns from ".MySQL::TABLE_OBJECTS.";");
                    $objects=array();
                    foreach(PropertiesManager::properties() as $property) {
                    //foreach($table as $row) {
                        if(!$property->hasValue && !$property->isValue) {
                        //if(substr($row["Field"],0,4)=="prop") {
                            $vars=array();
                            foreach($ids as $var) {
                                //array_push($vars,"`".$row["Field"]."`='".$var."'");
                                array_push($vars,"`".$property->field."`='".$var."'");
                            }
                        }
                    }
                            //if(count($vars)>0) {
                                $foundObjects=MySQLGet::process("Select `id` from ".MySQL::TABLE_OBJECTS." where ".implode(" OR ",$vars)." ".((count($vars)>0)?"or":"")." `text1` like '%$value%' or `text2` like '%$value%' or `text3` like '%$value%' or `text4` like '%$value%' or `text5` like '%$value%' and `active`='1';");
                                //trace("bubu: ".MySQLGet::getQuery());
                                if(MySQLGet::$cc>0) {
                                    foreach($foundObjects as $obj) {
                                        //trace("Found object: ".$obj['id']);
                                        array_push($objects,MySQL::TABLE_OBJECTS.".`id`='".$obj['id']."'");
                                    }
                                }
                            //}
                        //}
                    //}
                    array_push($values,"(".implode(" OR ", array_unique($objects)).")");
                }
            }
        //}
        if(count($values)>0) self::$extraWhere.=implode(" AND ", $values);
    }

    private static function searchWord($s) {
        $table=MySQLGet::process("Show columns from ".MySQL::TABLE_VALUES.";");
        $fields=array();
        foreach($table as $row) {
            if($row["Field"]!="id" && $row["Field"]!="system") {
                array_push($fields,"`".$row["Field"]."` like '%$s%'");
            }
        }
        /*$_columns=array("`v`.`lang1`","`v`.`lang2`","`v`.`lang3`","`v`.`lang4`","`v`.`lang5`","`o`.`text1`","`o`.`text2`","`o`.`text3`","`o`.`text4`","`o`.`text5`");
        $fields=array();
        foreach($_columns as $f->$value) {
            array_push($fields,$value."` like '%$s%'");
        }*/
        $result=MySQLGet::process("Select `id` from ".MySQL::TABLE_VALUES." where ".implode(" OR ", $fields).";");
        
        if(MySQLGet::$cc==0) return false;
        $resultArray=array();
        foreach($result as $row) {
            //trace("Found row: ".$row["id"]);
            array_push($resultArray,$row["id"]);
        }
        return $resultArray;
    }

}
?>
